<template>
  <div class="service-contact-table-container" v-loading="loading">
    <el-table
      stripe
      border
      :data="serviceList"
      :highlight-current-row="false"
      header-row-class-name="service-contact-table-header"
      row-class-name="service-contact-table-row"
      class="service-contact-table"
    >
      <el-table-column
        v-for="(column, index) in columns"
        v-if="column.show"
        :key="`${column.field}_${index}`"
        :label="column.label"
        :prop="column.field"
        :width="column.width"
        min-width="150px"
        :sortable="column.sortable"
        show-overflow-tooltip
        :align="column.align"
      >
        <template slot-scope="scope">
          <!--工单编号-->
          <template v-if="column.field === 'taskNo'">
            <div class="link" @click="gotoDetail('pageTaskView', scope.row.id, 'canViewTask=1')"> {{ scope.row.taskNo || '' }}</div>
          </template>
           <!--工单状态-->
          <template v-else-if="column.field === 'taskState'">
            <span v-if="scope.row.attribute && scope.row.attribute.lastPassedNodeStateName">
              {{scope.row.attribute.lastPassedNodeStateName}}
            </span>
            <span v-else>
              {{scope.row.taskState || ''}}
            </span>
          </template>
          <!--客户名称-->
          <template v-else-if="column.field === 'customerName'">
            <div class="link" @click="gotoDetail('pageCustomerView', scope.row.customerId, 'noHistory=1')"> {{ scope.row.customerName || '' }}</div>
          </template>
          <!--产品名称-->
          <template v-else-if="column.field === 'productNames'">
            <div class="link"
                 v-for="(item, index) in scope.row.products"
                 :key="`${index}_${item.id}`"
                 @click="gotoDetail('pageProductView', item.id, 'noHistory=1')"
            >
              <span v-if="index !== 0">、</span> {{ item.name || '' }}
            </div>
          </template>
          <template v-else>
            {{ scope.row[column.field] }}
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="service-table-footer">
      <p class="total-count">
        <i18n path="common.base.table.totalCount" tag="span">
          <span place="count" class="level-padding">{{ paginationInfo.totalItems }}</span>
        </i18n>
      </p>
      <el-pagination
        class="service-contact-table-pagination"
        v-if="paginationInfo.totalItems"
        background
        @current-change="jump"
        :page-size="paginationInfo.pageSize"
        :current-page="paginationInfo.pageNum"
        layout="prev, pager, next"
        :total="paginationInfo.totalItems"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
/* api */
import * as ServiceProviderApi from '@src/api/serviceProviderApi';
import { useTenantId } from '@hooks/useRootWindow.ts';
import { formatDate } from 'pub-bbx-utils';
import { openAccurateTab } from '@src/util/platform'
export default {
  name: 'service-task-table',
  props: {
    serviceProviderId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      serviceList: [],
      columns: [],
      paginationInfo: {
        pageSize: 10,
        pageNum: 1,
        totalItems: 0
      },
      loading: false,
    }
  },
  mounted() {
    this.columns = this.buildTaskColumns();
    this.fetchData();
  },
  methods: {
    // 打开工单，客户，产品详情页面
    gotoDetail(type, key, params = '') {
      let fromId = window.frameElement.getAttribute('id');
      openAccurateTab({
        type,
        key,
        params
      })
    },
    buildTaskColumns() {
      let columns = [
        {
          label: this.$t('common.label.taskNo'),
          field: 'taskNo',
          exportAlias: 'taskNo',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.taskTemplate'),
          field: 'taskTypeName',
          exportAlias: 'taskTypeName',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.customerName'),
          field: 'customerName',
          exportAlias: 'customerName',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.productName'),
          field: 'productNames',
          exportAlias: 'productNames',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.taskStatus'),
          field: 'taskState',
          exportAlias: 'taskState',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.createTime'),
          field: 'createTime',
          exportAlias: 'createTime',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.finishTime'),
          field: 'completeTime',
          exportAlias: 'completeTime',
          show: true,
          width: 160
        },
        {
          label: this.$t('common.label.customerEvaluation'),
          field: 'degree',
          exportAlias: 'degree',
          show: true,
          width: 160
        },
      ];
      return columns;
    },
    jump(pN) {
      this.paginationInfo.pageNum = pN;
      this.fetchData();
    },
    async fetchData() {
      let tenantId = useTenantId()?.value;
      let data = {
        pageNum: this.paginationInfo.pageNum,
        pageSize: this.paginationInfo.pageSize,
        serviceProviderId: this.serviceProviderId,
        tenantId: tenantId,
      }
      try {
        this.loading = true;
        let res = await ServiceProviderApi.getProviderTaskList(data)
        if(res.success && res.code === 0) {
          this.serviceList = res?.result?.list || [];
          this.serviceList = res?.result?.list?.map(item => {
            item.createTime = item.createTime && formatDate(item.createTime);
            item.completeTime = item.completeTime && formatDate(item.completeTime);
            return Object.freeze(item);
          });
          this.paginationInfo.totalItems = res?.result?.total;
        }else {
          this.$message.error(res.message)
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.service-contact-table-container {
  padding: 16px;
  .service-contact-table {
    border: 1px solid #ebeef5;
    height: 100%;
    .link {
      cursor: pointer;
      color:$color-primary;
    }
  }
  .service-contact-table-header th {
    background: #f5f5f5;
    color: $text-color-primary;
  }
  .service-contact-table-pagination {
    text-align: right;
    margin-top: 7px;
  }

  .service-table-footer {
    display: flex;
    justify-content: space-between;

    .total-count {
      padding: 0 10px;
      font-size: 12px;
      margin: 0;
      line-height: 46px;
      span {
        padding: 0 5px;
        @include fontColor();
      }
    }
  }
}
</style>